/*
 * Copyright (C) 2011 PickleMan <admin.aegamesi@gmail.com>
 * Copyright (C) 2011 IsmAvatar <IsmAvatar@gmail.com>
 * 
 * This file is part of Jeie.
 * 
 * Jeie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Jeie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License (COPYING) for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.jeie.util;

import java.awt.Graphics;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import javax.swing.ImageIcon;

public final class Util {
	private Util() {
	}

	public static ImageIcon getIconForKey(String key) {
		Properties iconProps = new Properties();
		InputStream is = Util.class.getClass().getResourceAsStream(
			"/org/jeie/main/icons.properties"); //$NON-NLS-1$
		try {
			iconProps.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String filename = iconProps.getProperty(key, null);
		if (filename != null && !filename.isEmpty())
			return getIcon(filename);
		return null;
	}

	/** Normally you would use getIconForKey for e13n */
	public static ImageIcon getIcon(String name) {
		return new ImageIcon(Util.class.getClass().getResource(
			"/org/jeie/icons/" + name)); //$NON-NLS-1$
	}

	/**
	 * Normally you would use getIconForKey for e13n.
	 * This method attempts to load externally first (e.g. non-jar), and then internally.
	 */
	public static ImageIcon findIcon(String filename) {
		String location = "org/jeie/icons/" + filename; //$NON-NLS-1$
		ImageIcon ico = new ImageIcon(location);
		if (ico.getIconWidth() == -1) {
			URL url = Util.class.getClass().getResource("/" + location); //$NON-NLS-1$
			if (url != null) {
				ico = new ImageIcon(url);
			}
		}
		return ico;
	}

	public static void drawGoodOval(Graphics g, int x1, int y1, int x2, int y2) {
		int w = x2 - x1;
		int h = y2 - y1;
		if (w < 0 && h < 0) {
			w = Math.abs(w);
			h = Math.abs(h);
			g.drawOval(x2, y2, w, h);
		} else if (w < 0) {
			w = Math.abs(w);
			g.drawOval(x2, y1, w, h);
		} else if (h < 0) {
			h = Math.abs(h);
			g.drawOval(x1, y2, w, h);
		} else {
			g.drawOval(x1, y1, w, h);
		}
	}

	public static void drawGoodRect(Graphics g, int x1, int y1, int x2, int y2) {
		int w = x2 - x1;
		int h = y2 - y1;
		if (w < 0 && h < 0) {
			w = Math.abs(w);
			h = Math.abs(h);
			g.drawRect(x2, y2, w, h);
		} else if (w < 0) {
			w = Math.abs(w);
			g.drawRect(x2, y1, w, h);
		} else if (h < 0) {
			h = Math.abs(h);
			g.drawRect(x1, y2, w, h);
		} else {
			g.drawRect(x1, y1, w, h);
		}
	}

	public static void fillGoodOval(Graphics g, int x1, int y1, int x2, int y2) {
		int w = x2 - x1;
		int h = y2 - y1;
		if (w < 0 && h < 0) {
			w = Math.abs(w);
			h = Math.abs(h);
			g.fillOval(x2, y2, w, h);
		} else if (w < 0) {
			w = Math.abs(w);
			g.fillOval(x2, y1, w, h);
		} else if (h < 0) {
			h = Math.abs(h);
			g.fillOval(x1, y2, w, h);
		} else {
			g.fillOval(x1, y1, w, h);
		}
	}

	public static void fillGoodRect(Graphics g, int x1, int y1, int x2, int y2) {
		int w = x2 - x1;
		int h = y2 - y1;
		if (w < 0 && h < 0) {
			w = Math.abs(w);
			h = Math.abs(h);
			g.fillRect(x2, y2, w, h);
		} else if (w < 0) {
			w = Math.abs(w);
			g.fillRect(x2, y1, w, h);
		} else if (h < 0) {
			h = Math.abs(h);
			g.fillRect(x1, y2, w, h);
		} else {
			g.fillRect(x1, y1, w, h);
		}
	}
}
